(function($) {
    var dataArray;
    var $rightContent = $('<div class="rightContent gridly"></div>');

    function createDom(leftMenu) {
        console.log(leftMenu);
        var data, dataArr = [];
        $(".show .rightContent .canMove").each(function() {
            data = {
                user: document.cookie.split('=')[1],
                leftMenu: leftMenu,
                modules: $(this).attr("data-module"),
                left: parseInt($(this).css("left")),
                top: parseInt($(this).css("top")),
            };
            switch (data.modules) {
                case "form":
                    data.formType = $(this).find("input").attr("type");
                    data.inputLabel = ($(this).find("label").length===0)?"no":"yes";
                    break;
                case "table":
                    data.tableTextRow = $(this).attr("data-row");
                    data.tableTextColumn = $(this).attr("data-column");
                    data.tableTs = $(this).attr("data-ts");
                    break;
                case "textarea":
                    data.textareaWidth = $(this).find("textarea").css("width");
                    data.textareaHight = $(this).find("textarea").css("height");
                    break;
                case "list":
                    break;
            }
            console.log(data);
            dataArr.push(data);
        });
        return dataArr;
    }
    var communication = function(leftMenu) {
        dataArray = createDom(leftMenu);
        $.ajax({
            type: 'post',
            url: '/operate/status',
            data: {
                user: document.cookie.split('=')[1],
                dataArray: dataArray,
                leftMenu: leftMenu
            },
            success: function(req) {
                console.log(req);
            }
        });
    };
    var initializeRight = function(leftMenu) {
        // dataArray = createDom();
        $.ajax({
            type: 'post',
            url: '/operate/initializeRight',
            data: {
                user: document.cookie.split('=')[1],
                // dataArray: dataArray,
                leftMenu: leftMenu
            },
            success: function(req) {
                $(".show .rightBox").html($rightContent);
                if (req === "empty") {
                    $rightContent.html("");
                } else {
                    $rightContent.html(req);
                }
                $('.gridly').gridly();
            }
        });
    }
    return (
        $.communication = communication,
        $.initializeRight = initializeRight
    );
})(jQuery);